package com.mj;

import com.mj.printer.BinaryTrees;
import com.mj.printer.Printer;

import java.nio.file.Files;
import java.util.Comparator;

public class Main {

    private static class personConstruct implements Comparator<Person> {

        @Override
        public int compare(Person e1, Person e2) {
            return e1.getAge() - e2.getAge();
        }
    }

    private static class personConstruct2 implements Comparator<Person> {

        @Override
        public int compare(Person e1, Person e2) {
            return e2.getAge() - e1.getAge();
        }
    }

    static void test1() {
        Integer[] data = {7, 4, 9, 2, 5, 8, 11, 3, 12, 1};
        //基本类型都默认有实现 Comparable接口
        BinarySearchTree<Integer> bst = new BinarySearchTree<>();

        for (int i = 0; i < data.length; i++) {
            bst.add(data[i]);
        }

        BinaryTrees.println(bst);
    }

    static void test2() {

        BinarySearchTree<Person> bst2 = new BinarySearchTree<>();
        BinarySearchTree<Person> bst3 = new BinarySearchTree<>(new Comparator<Person>() {
            @Override
            public int compare(Person o1, Person o2) {
                return o2.getAge() - o1.getAge();
            }
        });

        Integer[] data = {7, 4, 9, 2, 5, 8, 11, 3, 12, 1};

        for (int i = 0; i < data.length; i++) {
            bst2.add(new Person(data[i]));
            bst3.add(new Person(data[i]));
        }
        BinaryTrees.println(bst2);
        BinaryTrees.println(bst3);
    }

    static void test3() {
        Integer[] data = {7, 4, 9, 2, 5};
        //基本类型都默认有实现 Comparable接口
        BinarySearchTree<Integer> bst = new BinarySearchTree<>();
        for (int i = 0; i < data.length; i++) {
            bst.add(data[i]);
        }
        BinaryTrees.println(bst);
        System.out.println(bst.isComplete());
//        System.out.println(bst.height());
//        System.out.println(bst);
//        bst.levelTraversal(new BinarySearchTree.Visitor<Integer>() {
//            @Override
//            public void visit(Integer element) {
//                System.out.print("_" + element + "_");
//            }
//        });
    }

    static void test4() {
        BinarySearchTree<Integer> bst = new BinarySearchTree<>();
        for (int i = 0; i < 20; i++) {
            bst.add((int) (Math.random() * 100));
        }

        BinaryTrees.println(bst);
        System.out.println(bst.height());
    }

    static void test7() {
        BinarySearchTree<Integer> bst2 = new BinarySearchTree<>();
        Integer[] data = {7, 4, 9, 2, 1};

        for (int i = 0; i < data.length; i++) {
            bst2.add(data[i]);
        }
        BinaryTrees.println(bst2);

        bst2.preorder(new BinarySearchTree.Visitor<Integer>() {
            @Override
            public boolean visit(Integer element) {
                System.out.print(element + " ");
                return element == 2 ? true : false;
            }
        });
        System.out.println();

        bst2.inorderTraversal(new BinarySearchTree.Visitor<Integer>() {
            @Override
            public boolean visit(Integer element) {
                System.out.print(element + " ");
                 return element == 4 ? true : false;
            }
        });
        System.out.println();

        bst2.postorderTraversal(new BinarySearchTree.Visitor<Integer>() {
            @Override
            public boolean visit(Integer element) {
                System.out.print(element + " ");
                 return element == 4 ? true : false;
            }
        });
        System.out.println();

        bst2.levelTraversal(new BinarySearchTree.Visitor<Integer>() {
            @Override
            public boolean visit(Integer element) {
                System.out.print(element + " ");
                 return element == 9 ? true : false;
            }
        });
        System.out.println();

    }

    public static void main(String[] args) {
        test7();
    }

}
